﻿using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Admin;
using Repository.WXProjectAdmin;
using Xproject.Util;
using Model.WXshopBuiness;
using System;
using System.Collections.Generic;
using Model.Admin.DTO;
using System.Linq;
using System.Linq.Expressions;

namespace Services.WXProjectAdmin
{
   public  class SystemMenuServices:Service
    {
        IRepositoryAsync<AdminMenu> m_AdminMenuRepository;
        IRepositoryAsync<AdminRoleMenu> m_AdminRoleMenuRepository;
        IRepositoryAsync<AdminUserRole> m_AdminUserRoleRepository;
        public SystemMenuServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_AdminMenuRepository = m_UnitOfWork.RepositoryAsync<AdminMenu>();
            m_AdminRoleMenuRepository = m_UnitOfWork.RepositoryAsync<AdminRoleMenu>();
            m_AdminUserRoleRepository = m_UnitOfWork.RepositoryAsync<AdminUserRole>();
        }

        public bool AddMenu(AdminMenu menu,string  userId) {
            if (menu != null)
            {
                menu.ID = Guid.NewGuid().ToString();                 
                menu.EntityState = EntityState.Added;
                menu.CreateTime = DateTime.Now;
                menu.UpdateTime = DateTime.Now;
                menu.Creator = menu.Updator = userId;
                m_AdminMenuRepository.Insert(menu);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        /// <summary>
        /// 删除最好不做硬删除  做软删除
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool DelMenu(AdminMenu menu)
        {
            if (menu != null)
            {
                menu.EntityState = EntityState.Deleted;
                m_AdminMenuRepository.Delete(menu);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        public bool EditMenu(AdminMenu menu, string userId)
        {
            if (menu != null)
            {                
                menu.EntityState = EntityState.Modified;              
                menu.UpdateTime = DateTime.Now;
                menu.Updator = userId.ToString();
                m_AdminMenuRepository.Update(menu);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        /// <summary>
        /// 通过menuId得到
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public AdminMenu GetAdminMenuByMenuId(int  menuId)
        {
            return m_AdminMenuRepository.GetByMenuId(menuId);
        }
        public List<AdminMenu> GetAdminMenuList(string site)
        {
            if (Cache_SystemInfo.AdminMenus == null)
            {
                List<AdminMenu> temp = m_AdminMenuRepository.GetAllMenuList(site, null, null);
                if (temp != null && temp.Count > 0)
                {
                    Cache_SystemInfo.AdminMenus = temp;
                }
            }
            return Cache_SystemInfo.AdminMenus;
        }
        /// <summary>
        /// 得到某个站点平台的菜单信息
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public List<AdminMenu> GetAdminMenuList(string site,int offset, int pageSize,string menuName,string menuDesc,out int total) {
            total = 0;
            if (Cache_SystemInfo.AdminMenus == null)
            {
                List<AdminMenu> temp =  m_AdminMenuRepository.GetAllMenuList(site,null,null);
                if(temp!=null && temp.Count > 0)
                { 
                    Cache_SystemInfo.AdminMenus = temp;
                }
            }
            
            if (Cache_SystemInfo.AdminMenus != null && Cache_SystemInfo.AdminMenus.Count > 0)
            {
               
                List<AdminMenu> temp = Cache_SystemInfo.AdminMenus;
                if (!string.IsNullOrEmpty(menuName)) {
                    temp = Cache_SystemInfo.AdminMenus.Where(i => i.MenuName.Contains(menuName)).ToList();
                }
                if (!string.IsNullOrEmpty(menuDesc))
                {
                    temp = Cache_SystemInfo.AdminMenus.Where(i => i.MenuDesc.Contains(menuDesc)).ToList();
                }
                total = temp.Count;
                return  temp.Skip(offset).Take(pageSize).ToList();

            }
            return Cache_SystemInfo.AdminMenus;
        }
        /// <summary>
        /// 取某一级别的所有菜单
        /// </summary>
        /// <param name="site"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public List<AdminMenu> GetLevelMenuList(string site, int level)
        {
            if (Cache_SystemInfo.AdminMenus == null)
            {
                List<AdminMenu> temp = m_AdminMenuRepository.GetAllMenuList(site,null,null);
                if (temp != null && temp.Count > 0)
                {
                    Cache_SystemInfo.AdminMenus = temp;
                }
            }
            if (Cache_SystemInfo.AdminMenus != null && Cache_SystemInfo.AdminMenus.Count > 0)
            {
               return  Cache_SystemInfo.AdminMenus.Where(i => i.MenuLevel == level && i.Enable==1 && i.Site==site).ToList();
            }
            return null;
        }
        /// <summary>
        /// 根据权限得到用户的菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<AdminMenuInfo> GetUserMenuInfoList(int userId)
        {
            return null;
        }
        /// <summary>
        /// 得到角色所有的菜单id
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<AdminRoleMenu> GetRoleMenus(int roleID)
        {
            return m_AdminRoleMenuRepository.GetRoleMenus(roleID);
        }
        public List<AdminRoleMenu> GetRoleMenus(List<int> roleID)
        {
            return m_AdminRoleMenuRepository.GetRoleMenus(roleID);
        }
        /// <summary>
        /// 得到用户菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<AdminMenu> GetUserMenus(int userId)
        {
            //先得到用户的角色
            List<AdminUserRole> userroles= m_AdminUserRoleRepository.GetUserRoles(userId);
            if (userroles != null&& userroles.Count>0)
            {
                List<int> roleids = userroles.Select(i => i.RoleID).Distinct().ToList();
                if (roleids != null && roleids.Count > 0)
                {
                    List<AdminRoleMenu> rolemenuids = GetRoleMenus(roleids);
                    if (rolemenuids != null && rolemenuids.Count > 0)
                    {
                        List<int> menuids = rolemenuids.Select(i => i.MenuId).Distinct().ToList();
                        if (menuids != null && menuids.Count > 0)
                        {
                           return  Cache_SystemInfo.AdminMenus.Where(i => menuids.Contains(i.MenuId)).ToList();
                        }                        
                    }                    
                }
            }
            return null;                        
        }
        /// <summary>
        /// 更新角色的菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="newRoleMenus"></param>
        /// <returns></returns>
        public bool UpdateRoleMenus(int roleId, List<AdminRoleMenu> newRoleMenus) {
            //先得到角色所有的菜单
            List<AdminRoleMenu> oldMenus =  m_AdminRoleMenuRepository.GetRoleMenus(roleId);
            foreach (AdminRoleMenu arm in oldMenus)
            {
                arm.EntityState = EntityState.Deleted;
            }
            foreach (AdminRoleMenu arm in newRoleMenus)
            {
                arm.EntityState = EntityState.Added;
            }
            m_UnitOfWork.BeginTransaction();
            m_AdminRoleMenuRepository.DeleteRange(oldMenus);
            m_AdminRoleMenuRepository.InsertRange(newRoleMenus);
            int result = m_UnitOfWork.SaveChanges();
            if (result == oldMenus.Count + newRoleMenus.Count)
            {
                try
                {
                    m_UnitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    m_UnitOfWork.Rollback();
                    return false;
                }
                return true;
            }
            else
            {
                m_UnitOfWork.Rollback();
                return false;
            }
        }

        
    }
}
